package com.governtalk.backend.controller;

import com.governtalk.backend.dto.question.QuestionDto;
import com.governtalk.backend.dto.question.QuestionCategoryDto;
import com.governtalk.backend.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

@RestController
@RequestMapping("/api/questions")
public class QuestionController {
    
    @Autowired
    private QuestionService questionService;
    
    @GetMapping("/categories")
    public ResponseEntity<List<QuestionCategoryDto>> getQuestionCategories() {
        List<QuestionCategoryDto> categories = questionService.getQuestionCategories();
        return ResponseEntity.ok(categories);
    }
    
    @GetMapping("/search")
    public ResponseEntity<Page<QuestionDto>> searchQuestions(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String difficulty,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) String sortBy,
            @RequestParam(required = false) String sortOrder,
            Pageable pageable) {
        Page<QuestionDto> questions = questionService.searchQuestions(keyword, type, difficulty, categoryId, sortBy, sortOrder, pageable);
        return ResponseEntity.ok(questions);
    }
    
    @GetMapping("/{questionId}")
    public ResponseEntity<QuestionDto> getQuestionDetail(@PathVariable Long questionId) {
        QuestionDto question = questionService.getQuestionDetail(questionId);
        return ResponseEntity.ok(question);
    }
    
    @PostMapping("/{questionId}/answer")
    public ResponseEntity<Map<String, Object>> submitAnswer(
            @PathVariable Long questionId,
            @RequestBody Map<String, Object> answer) {
        Map<String, Object> result = questionService.submitAnswer(questionId, answer);
        return ResponseEntity.ok(result);
    }
    
    @PostMapping("/{questionId}/favorite")
    public ResponseEntity<Map<String, Boolean>> favoriteQuestion(@PathVariable Long questionId) {
        questionService.favoriteQuestion(questionId);
        Map<String, Boolean> response = new HashMap<>();
        response.put("isFavorite", true);
        return ResponseEntity.ok(response);
    }
    
    @GetMapping("/favorites")
    public ResponseEntity<Page<QuestionDto>> getFavoriteQuestions(Pageable pageable) {
        Page<QuestionDto> questions = questionService.getFavoriteQuestions(pageable);
        return ResponseEntity.ok(questions);
    }
    
    @GetMapping("/history")
    public ResponseEntity<Page<Map<String, Object>>> getAnswerHistory(Pageable pageable) {
        Page<Map<String, Object>> history = questionService.getAnswerHistory(pageable);
        return ResponseEntity.ok(history);
    }
    
    @PostMapping
    public ResponseEntity<QuestionDto> createQuestion(@RequestBody QuestionDto questionDto) {
        QuestionDto question = questionService.createQuestion(questionDto);
        return ResponseEntity.ok(question);
    }
    
    @PutMapping("/{questionId}")
    public ResponseEntity<QuestionDto> updateQuestion(
            @PathVariable Long questionId,
            @RequestBody QuestionDto questionDto) {
        QuestionDto question = questionService.updateQuestion(questionId, questionDto);
        return ResponseEntity.ok(question);
    }
    
    @DeleteMapping("/{questionId}")
    public ResponseEntity<String> deleteQuestion(@PathVariable Long questionId) {
        questionService.deleteQuestion(questionId);
        return ResponseEntity.ok("Question deleted successfully");
    }
    
    @PostMapping("/import")
    public ResponseEntity<Map<String, Object>> importQuestions(@RequestParam("file") MultipartFile file) {
        Map<String, Object> result = questionService.importQuestions(file);
        return ResponseEntity.ok(result);
    }
    
    // 在线练习相关接口
    @GetMapping("/practice")
    public ResponseEntity<List<QuestionDto>> getPracticeQuestions(
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) Integer difficulty,
            @RequestParam(required = false) String type,
            @RequestParam(defaultValue = "10") Integer count) {
        List<QuestionDto> questions = questionService.getPracticeQuestions(categoryId, difficulty, type, count);
        return ResponseEntity.ok(questions);
    }
    
    @PostMapping("/practice/submit")
    public ResponseEntity<Map<String, Object>> submitPracticeAnswers(@RequestBody Map<String, Object> data) {
        Map<String, Object> result = questionService.submitPracticeAnswers(data);
        return ResponseEntity.ok(result);
    }
    
    @GetMapping("/practice/history")
    public ResponseEntity<Page<Map<String, Object>>> getPracticeHistory(Pageable pageable) {
        Page<Map<String, Object>> history = questionService.getPracticeHistory(pageable);
        return ResponseEntity.ok(history);
    }
    
    @PostMapping("/search/history")
    public ResponseEntity<Map<String, Object>> saveSearchHistory(@RequestBody Map<String, Object> data) {
        Map<String, Object> result = questionService.saveSearchHistory(data);
        return ResponseEntity.ok(result);
    }
    
    @GetMapping("/search/history")
    public ResponseEntity<Page<Map<String, Object>>> getSearchHistory(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<Map<String, Object>> history = questionService.getSearchHistory(pageable);
        return ResponseEntity.ok(history);
    }
    
    @GetMapping("/search/history/recent")
    public ResponseEntity<List<Map<String, Object>>> getRecentSearchHistory() {
        List<Map<String, Object>> history = questionService.getRecentSearchHistory();
        return ResponseEntity.ok(history);
    }
    
    @DeleteMapping("/search/history")
    public ResponseEntity<Map<String, Object>> clearSearchHistory() {
        Map<String, Object> result = questionService.clearSearchHistory();
        return ResponseEntity.ok(result);
    }
}
